package com.ts.animal.controller;

import com.ts.animal.domain.CustomerDO;
import com.ts.animal.service.CustomerService;
import com.ts.common.annotation.Log;
import com.ts.common.utils.PageInfos;
import com.ts.common.utils.PageUtils;
import com.ts.common.utils.Result;
import com.ts.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 客户表
 *
 * @author bobby
 * @email bobby@126.com
 * @date 2020-02-04 21:57:31
 */

@Controller
@RequestMapping("/animal/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @GetMapping()
    @RequiresPermissions("animal:customer:customer")
    String Customer() {
        return "animal/customer/customer";
    }

    @ResponseBody
    @RequestMapping("/list")
    @RequiresPermissions("animal:customer:customer")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        PageUtils pageUtils = new PageInfos(params) {
            public List inject(Map params) {
                return customerService.list(params);
            }
        }.query();
        return pageUtils;
    }

    @ResponseBody
    @GetMapping("/dict/list")
    public List<CustomerDO> dictList(@RequestParam Map<String, Object> params) {
        ShiroUtils.setDeptId(Long.valueOf(params.get("cDeptId").toString()));
        //查询列表数据
        List<CustomerDO> customerList = customerService.list(params);
        return customerList;
    }


    @GetMapping("/add")
    @RequiresPermissions("animal:customer:add")
    String add() {
        return "animal/customer/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("animal:customer:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        CustomerDO customer = customerService.get(id);
        model.addAttribute("customer", customer);
        return "animal/customer/edit";
    }

    /**
     * 保存
     */
    @Log("添加客户")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("animal:customer:add")
    public Result save(CustomerDO customer) {
        if (customerService.save(null, customer) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 修改
     */
    @Log("修改客户信息")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("animal:customer:edit")
    public Result update(CustomerDO customer) {
        customerService.update(null, customer);
        return Result.ok();
    }

    /**
     * 删除
     */
    @Log("删除客户信息")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("animal:customer:remove")
    public Result remove(Integer id) {
        if (customerService.remove(id) > 0) {
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除
     */
    @Log("批量删除客户信息")
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("animal:customer:batchRemove")
    public Result remove(@RequestParam("ids[]") Integer[] ids) {
        customerService.batchRemove(ids);
        return Result.ok();
    }

}
